using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DejaVuTocReader
{
    public abstract class InputStream 
    {
        

        public InputStream()
        {
        }

        public virtual int available()
        {
            return 0;
        }

        public virtual void close()
        {
        }

        public virtual void mark(int readLimit)
        {
        }

        public virtual bool markSupported()
        {
            return false;
        }

        public abstract int read();

        public virtual int read(byte[] b)
        {
           return read(b, 0, b.Length);
        }

        public int read(byte[] b,int off,int len)
        {
            int res = 0; //number of bytes actually read

            if (b == null)
            {
                throw new NullReferenceException();
            }

            if (len < 0 || off < 0 || (off + len) > b.Length)
            {
                throw new IndexOutOfRangeException();
            }

            int  n = 0;
            while ((n = read()) != -1 && ++res <= len)
            {
                b[off + res - 1] = (byte)n;
            }

            if (res <= len)
                return res;
            else return (res - 1);


        }

        public void reset()
        {
            throw new IOException("Reset not supported in C# InputStream");
        }

        public virtual long skip(long n)
        {
            MemoryStream ms = new MemoryStream();
            int m = 0;
            int i = 0;
            while ((m = read()) != -1 && (++i)<=n)
            {
                ms.WriteByte((byte)m);
            }

            return i;
        }

        protected object clone()
        {
            return base.MemberwiseClone();
        }

        
    }
}
